package com.example.fresh_market.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.fresh_market.R;
import com.example.fresh_market.adapter.OrderAdapter;
import com.example.fresh_market.constants.StrConstant;
import com.example.fresh_market.entity.Order;
import com.example.fresh_market.request.ApiResponse;
import com.example.fresh_market.request.PageResult;
import com.example.fresh_market.utils.HttpUtil;
import com.example.fresh_market.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Map;

import lombok.NoArgsConstructor;

/**
 * 用户订单页面
 */
@NoArgsConstructor
public class OrderFragment extends Fragment {

    private Context mContext;
    private OrderAdapter orderAdapter;

    public static OrderFragment newInstance() {
        OrderFragment fragment = new OrderFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, container, false);
        RecyclerView order_listView = view.findViewById(R.id.order_recycler);
        LinearLayoutManager llM = new LinearLayoutManager(mContext);
        order_listView.setLayoutManager(llM);
        orderAdapter = new OrderAdapter(new ArrayList<>(), new OrderAdapter.OnItemClickListener() {
            @Override
            public void onRefundClick(View view, int position) {
                Order order = orderAdapter.getItem(position);
                Map<String, String> params = Map.of("orderId", order.getId().toString());
                HttpUtil.post(mContext, "/usr/order/refund", params, response -> {
                    ApiResponse apiResponse = new Gson().fromJson(response, ApiResponse.class);
                    if (apiResponse.getCode() == 200) {
                        ToastUtil.showToast(mContext, "发起退款");
                        init();
                    }
                });
            }

            @Override
            public void onTakeClick(View view, int position) {
                Order order = orderAdapter.getItem(position);
                String url = "/usr/order/take/" + order.getId();
                HttpUtil.get(mContext, url, response -> {
                    ApiResponse apiResponse = new Gson().fromJson(response, ApiResponse.class);
                    if (apiResponse.getCode() == 200) {
                        ToastUtil.showToast(mContext, "已取货");
                        init();
                    }
                });
            }
        });
        order_listView.setAdapter(orderAdapter);
        init();
        return view;
    }

    private void init() {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(StrConstant.CONFIG, Context.MODE_PRIVATE);
        String token = sharedPreferences.getString(StrConstant.TOKEN, "");
        HttpUtil.get(mContext, "/usr/order/list?page=1&size=100000", Map.of(HttpUtil.Auth, token), response -> {
            ApiResponse<PageResult<Order>> apiResponse = new Gson().fromJson(response, new TypeToken<ApiResponse<PageResult<Order>>>() {
            }.getType());
            if (apiResponse.getCode() == 200) {
                orderAdapter.setData(apiResponse.getData().getList());
            }
        });
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            init();
        }
    }
}